<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessageBox } from 'element-plus'
import {
  HomeFilled,
  Bicycle,
  SwitchButton,
  Menu as MenuIcon,
  Setting,
  User,
  Document,
  Warning
} from '@element-plus/icons-vue'

const router = useRouter()
const userStore = useUserStore()

// 控制侧边栏折叠状态
const isCollapse = ref(false)

// 切换侧边栏折叠状态
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      userStore.logout()
      router.push('/login')
    })
    .catch(() => {
      // 取消退出
    })
}
</script>

<template>
  <el-container>
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="logo-container">
        <svg class="bicycle-icon" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
          <path
            d="M5,20.5A3.5,3.5 0 0,1 1.5,17A3.5,3.5 0 0,1 5,13.5A3.5,3.5 0 0,1 8.5,17A3.5,3.5 0 0,1 5,20.5M5,12A5,5 0 0,0 0,17A5,5 0 0,0 5,22A5,5 0 0,0 10,17A5,5 0 0,0 5,12M14.8,10H19V8.2H15.8L13.8,4.8C13.5,4.3 12.8,4 12,4C11.2,4 10.5,4.3 10.2,4.8L8.2,8.2H5V10H8.8L10.8,13.5C11.1,14 11.8,14.2 12.5,14.2C13.2,14.2 13.9,14 14.2,13.5L16.2,10H14.8M19,20.5A3.5,3.5 0 0,1 15.5,17A3.5,3.5 0 0,1 19,13.5A3.5,3.5 0 0,1 22.5,17A3.5,3.5 0 0,1 19,20.5M19,12A5,5 0 0,0 14,17A5,5 0 0,0 19,22A5,5 0 0,0 24,17A5,5 0 0,0 19,12M16,4.8C17,4.8 17.8,4 17.8,3C17.8,2 17,1.2 16,1.2C15,1.2 14.2,2 14.2,3C14.2,4 15,4.8 16,4.8Z"
            fill="#409EFF"
          />
        </svg>
        <h1 class="title">校园单车管理系统</h1>
      </div>
      <div class="header-right">
        <span class="welcome-text">欢迎，{{ userStore.userInfo?.name }}</span>
        <el-button type="danger" plain size="small" @click="handleLogout">
          <el-icon><SwitchButton /></el-icon>
          退出登录
        </el-button>
      </div>
    </el-header>

    <el-container>
      <!-- 侧边栏 -->
      <el-aside>
        <div class="sidebar-toggle" @click="toggleSidebar">
          <el-icon><MenuIcon /></el-icon>
        </div>
        <el-menu
          :collapse="isCollapse"
          default-active="/home"
          class="sidebar-menu"
          router
          background-color="#001529"
          text-color="#fff"
          active-text-color="#409EFF"
        >
          <!-- 管理员菜单 -->
          <template v-if="userStore.userInfo?.role === 'admin'">
            <el-menu-item index="/admin/bicycles">
              <el-icon><Bicycle /></el-icon>
              <template #title>单车管理</template>
            </el-menu-item>
            <el-menu-item index="/admin/maintenance">
              <el-icon><Setting /></el-icon>
              <template #title>维修管理</template>
            </el-menu-item>
            <el-menu-item index="/admin/statistics">
              <el-icon><Document /></el-icon>
              <template #title>数据统计</template>
            </el-menu-item>
          </template>

          <!-- 学生菜单 -->
          <template v-if="userStore.userInfo?.role === 'student'">
            <el-menu-item index="/student/borrow">
              <el-icon><Bicycle /></el-icon>
              <template #title>借用单车</template>
            </el-menu-item>
            <el-menu-item index="/student/report">
              <el-icon><Warning /></el-icon>
              <template #title>故障报修</template>
            </el-menu-item>
          </template>
        </el-menu>
      </el-aside>

      <!-- 主要内容区域 -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  padding: 0 20px;
  height: 60px;
}

.logo-container {
  display: flex;
  align-items: center;
}

.bicycle-icon {
  width: 32px;
  height: 32px;
  margin-right: 10px;
}

.title {
  font-size: 18px;
  color: #001529;
  margin: 0;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 15px;
}

.welcome-text {
  font-size: 14px;
  color: #606266;
}

.sidebar-toggle {
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #001529;
  color: #fff;
  cursor: pointer;
  transition: background-color 0.3s;

  &:hover {
    background-color: #1890ff;
  }
}

.sidebar-menu {
  height: calc(100vh - 110px);
  border-right: none;
}

.el-main {
  background-color: #f0f2f5;
  padding: 20px;
}
</style>
